﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;


namespace QLTV
{
    static class Program
    {
        public static string username { get; set; }
        public static string language { get; set; }
        public static string EmployeeName { get; set; }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            //LOG ERROR TO SYSTEM
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Sharp";
            //defaultLookAndFeel
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Forms.BaoMat.frmLogin logon = new Forms.BaoMat.frmLogin();
            // Application.Run(logon);
            Ultilities.Run(logon);

            if (logon.LogonSuccessful)
            {
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = new Database.dsUsersTableAdapters.tblUserProfileTableAdapter().getTheme(Program.username);
                if (Program.language == "vi-VN")
                {
                    DevExpress.XtraGrid.Localization.GridLocalizer.Active = new VietnamLocalization(new DevExpress.XtraGrid.Localization.GridResLocalizer());
                    DevExpress.XtraEditors.Controls.Localizer.Active = new vnTextEdit();
                    DevExpress.XtraLayout.Localization.LayoutLocalizer.Active = new vnLayoutControl();
                }
                Application.Run(new frmMain());
            }

        }

        #region HANDLE EXCEPTION

        //handle exception
        public static void Application_ThreadException(object sender, ThreadExceptionEventArgs t)
        {
            
            frmLoginSql f = new frmLoginSql();
            f.ShowDialog();



        }



        /// <summary>
        /// Write log
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {

            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                string errorMsg = "An application error occurred. Please contact the adminstrator " +
                    "with the following information:\n\n";

                // Since we can't prevent the app from terminating, log this to the event log.
                if (!EventLog.SourceExists("ThreadException"))
                {
                    EventLog.CreateEventSource("ThreadException", "Application");
                }

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "ThreadException";
                myLog.WriteEntry(String.Format("{0}{1}\n\nStack Trace:\n{2}", errorMsg, ex.Message, ex.StackTrace));
            }
            catch (Exception exc)
            {
                try
                {
                    MessageBox.Show("Fatal Non-UI Error",
                        "Fatal Non-UI Error. Could not write the error to the event log. Reason: "
                        + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                finally
                {
                    Application.Exit();
                }
            }
        }
        #endregion
    }
}
